/*
 * Copyright 2011 Fabian Wenzelmann
 * 
 * This file is part of Recognition-Procedures-for-Boolean-Functions.
 * 
 * Recognition-Procedures-for-Boolean-Functions is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Recognition-Procedures-for-Boolean-Functions is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Recognition-Procedures-for-Boolean-Functions.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.wenzelmf.booleanFunction.dnf;

import java.util.HashMap;
import java.util.Iterator;

import de.wenzelmf.booleanFunction.lpb.util.LPBCombinatorialConstants;

/**
 * This is a implementation of the {@link Clause} class which uses a HashMap to store the elements.
 * 
 * This implementation allows for each variable only <i>one</i> polarity, i.e. it is not allowed
 * that the variable <i>x</i> appears positive and negative in this clause.
 * 
 * If you add a Literal with a variable that is already present in this map the old occurrence gets
 * removed.
 */
public class MapClause extends Clause
{
	/*
	 * Maps for an identifier the corresponding literal, i.e. maps l.getIdentifier() to l.
	 */
	private HashMap<String, Literal> occurrences;
	
	/*
	 * The cached hash value of this class (sum over all literals in this clause).
	 */
	private int hashCode;
	
	/**
	 * Constructs an empty clause.
	 */
	public MapClause()
	{
		this.occurrences = new HashMap<String, Literal>();
		this.hashCode = 0;
	}
	
	/**
	 * Constructs an empty clause where the HashMap is created with the initial size.
	 *  
	 * @param initialSize The initial size of the HashMap.
	 */
	public MapClause(int initialSize)
	{
		this.occurrences = new HashMap<String, Literal>((int)(initialSize * LPBCombinatorialConstants.HASH_SIZE_FACTOR));
		this.hashCode = 0;
	}
	
	/**
	 * Cosntructs an empty clause where the HashMap is created with the initial size and load factor.
	 * 
	 * @param initialSize The initial size of the HashMap.
	 * @param loadFactor The load factor that is used for the HashMap.
	 */
	public MapClause(int initialSize, float loadFactor)
	{
		this.occurrences = new HashMap<String, Literal>((int)(initialSize * LPBCombinatorialConstants.HASH_SIZE_FACTOR),
				loadFactor);
		this.hashCode = 0;
	}
	
	/**
	 * Return the hash code of this object, that is the sum of all literal hash codes that are
	 * contained in this clause.
	 * 
	 * @return The hash code of this clause.
	 */
	@Override
	public int hashCode()
	{
		return hashCode;
	}

	/**
	 * Returns an iterator over all literals contained in this clause, removing elements by the
	 * {@code remove} method is not allowed, it just throws an {@code UnsupportedOperationException}.
	 * 
	 * @return An iterator over all literals in this clause.
	 */
	@Override
	public Iterator<Literal> iterator()
	{
		return new Iterator<Literal>()
		{
			
			Iterator<Literal> it = occurrences.values().iterator();
			
			@Override
			public boolean hasNext()
			{
				return it.hasNext();
			}

			@Override
			public Literal next()
			{
				return it.next();
			}

			@Override
			public void remove()
			{
				throw new UnsupportedOperationException("You are not allowed to remove elements from a MapClause while iterating over it.");
			}
			
		};
	}
	
	/**
	 * Returns true if this clause contains the specified object.
	 * 
	 * @param o The element to look for.
	 * @return {@code true} if this clause contains {@code o}.
	 */
	public boolean contains(Object o)
	{
		if(o == null)
			return false;
		if(!(o instanceof Literal))
			return false;
		Literal l = (Literal)o;
		Literal val = occurrences.get(l.getIdentifier());
		return l.equals(val);
	}

	@Override
	public boolean containsVariable(String name)
	{
		return occurrences.get(name) != null;
	}

	/**
	 * Add the specified Literal to this clause.
	 * 
	 * @param l The literal to add.
	 * @return {@code true} if the clause was modified as a result of this action.
	 */
	@Override
	public boolean add(Literal l)
	{
		Literal old = occurrences.put(l.getIdentifier(), l);
		if(old != null)
		{
			this.hashCode -= old.hashCode();
		}
		this.hashCode += l.hashCode();
		return old == null;
	}
	
	/**
	 * Remove the specified object from this clause.
	 * 
	 * @param o the object to remove.
	 * 
	 * @return {@code true} if the collection changed as a result of this call.
	 */
	@Override
	public boolean remove(Object o)
	{
		if(o == null)
			return false;
		if(!(o instanceof Literal))
			return false;
		Literal l = (Literal)o;
		Literal removed = occurrences.remove(l.getIdentifier());
		if(removed != null)
		{
			this.hashCode -= removed.hashCode();
			return true;
		}
		return false;
	}

	/**
	 * Return the size of the clause, i. e. the amount of literals in it.
	 * 
	 * @return The size of this clause.
	 */
	@Override
	public int size()
	{
		return occurrences.size();
	}
	
}
